import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AES {

    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static String PASS_WORD = "xyczI160Ry9j70JPSJcm9Da3KB6udl";
    private static final String SIGN_ALGORITHMS = "SHA1PRNG";


    /**
     * AES encode
     * @param content
     * @return 
     */
    public static String encrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(PASS_WORD));
            byte[] result = cipher.doFinal(byteContent);
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    /**
     * AES decode
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(PASS_WORD));
            byte[] bytes = Base64.getDecoder().decode(content);
            byte[] result = cipher.doFinal(bytes);
            return new String(result, "utf-8");

        } catch (Exception ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    /**
     * generate key
     * @return
     */
    private static SecretKeySpec getSecretKey(final String password) {
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
            random.setSeed(password.getBytes());
            kg.init(128, random);
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    public static void main(String[] args) {
        if(args.length > 0){
            String[] data = args[0].split(",");
			if(args.length == 2){PASS_WORD = args[1];};

            if(data[0].matches("(.*)==(.*)")){
                for (String v: data){
                    System.out.println(AES.decrypt(v));
                }                
            }else{
                for (String v: data){
                    System.out.println(AES.encrypt(v));
                }                  
            }
        }else{
            System.out.println("fail");
        }
    }

}

